package com.nekonomics
package api.route

import zio.ZIO
import zio.http.Method.*
import zio.http.*
import zio.http.codec.PathCodec

object TestRoute {

  lazy val routes = PathCodec.literal("test") /  Routes(
    // http://localhost:8080/test/hello/dasd
    GET / "hello" / string("name") -> handler { (name: String, req: Request) =>
      Response.text(s"Hello $name")
    },


    // http://localhost:8080/test/greet?name=dasd
    GET / "greet" -> handler { (req: Request) =>
      if (req.url.queryParams.nonEmpty)
        ZIO.succeed(
          Response.text(
            s"Hello ${req.url.queryParams("name").map(_.mkString(" and "))}!"
          )
        )
      else
        ZIO.fail(Response.badRequest("The name query parameter is missing!"))
    },

  )

}
